# Base Define
CC = /homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/bin/clang --sysroot=/homeB/minjie.yu/openharmony/ipcamera/clang-10/musl
CXX = /homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/bin/clang++ --sysroot=/homeB/minjie.yu/openharmony/ipcamera/clang-10/musl
AR = /homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/bin/llvm-ar
LD = /homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/bin/llvm-ld
STRIP = /homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/bin/llvm-strip
ARFLAG = -rcs

CFLAGS := -march=armv7-a \
	-mfloat-abi=softfp \
	-mtune=generic-armv7-a \
	-mfpu=neon \
	-mthumb \
	--target=arm-linux-ohosmusl \
	--sysroot=/homeB/minjie.yu/openharmony/ipcamera/clang-10/musl

VIVANTE_SDK_DIR := $(SDK_DIR)
VIVANTE_SDK_INC := $(SDK_DIR)/usr/include
VIVANTE_SDK_LIB := $(SDK_DIR)/lib

# Source
SRC_ROOT := $(shell pwd)

# Bulid Info
BUILD_TIME := $(shell date +%Y%m%d)

# Include Path
INCLUDE_PATH := -I$(SRC_ROOT)/

INCLUDE_PATH += -I$(SRC_ROOT)/../utils
INCLUDE_PATH += -I$(SRC_ROOT)/../../include
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../drivers/framework/include/utils
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../drivers/adapter/uhdf/posix/include
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../drivers/framework/include/core
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../drivers/peripheral/display/interfaces/include
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../drivers/peripheral/base
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../base/hiviewdfx/hilog_lite/interfaces/native/kits
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../third_party/libdrm/
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../third_party/libdrm/include/drm
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../third_party/libdrm/include
INCLUDE_PATH += -I$(SRC_ROOT)/../../../../../third_party/bounds_checking_function/include

INCLUDE_PATH += -I/homeB/minjie.yu/openharmony/ipcamera/clang-10/musl/usr/include/arm-linux-ohos

# App cflags
APP_CFLAGS := -Wall -D_LINUX_ -D_GNU_SOURCE -Wno-incompatible-library-redeclaration
APP_LIBS := -lm -lpthread -ldl

CXXFLAGS := 

LINK_FLAGS := -L/homeB/minjie.yu/openharmony/ipcamera/clang-10/musl/usr/lib/arm-linux-ohos -L/homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/lib/arm-linux-ohos  -L/homeB/minjie.yu/openharmony/ipcamera/clang-10/clang/lib/arm-linux-ohos/c++ -Wl,--exclude-libs=libunwind_llvm.a -Wl,--exclude-libs=libc++_static.a -Wl,--exclude-libs=libvpx_assembly_arm.a -Wl,--warn-shared-textrel --rtlib=compiler-rt

LINK_FLAGS += -L/homeB/minjie.yu/openharmony/ipcamera/ipcamera/prebuilt/lib -lsec_shared -lhilog_shared -ldrm -ldisplay_gralloc

#CFLAGS += -g -pipe

APP_CFLAGS += $(CFLAGS)

# Common Libs
COMM_LIBS := -L$(SRC_ROOT)

# App Libs
APP_LIBS += $(COMM_LIBS)

# Path Define
SRC_ROOT	:= $(SRC_ROOT)
OBJ_ROOT	:= $(SRC_ROOT)/.OBJ
EXE_ROOT	:= $(SRC_ROOT)

# Target
NAME := libhdi_videodisplayer
LIB := $(EXE_ROOT)/$(NAME).so
EXE := $(EXE_ROOT)/$(NAME)

# Source and Object File 
# Cpp Source
SRCS_CPP      := $(shell find $(SRC_ROOT) -name '*.cpp')
SRCS_CPP      := $(sort $(SRCS_CPP))

OBJS_CPP      := $(SRCS_CPP:$(SRC_ROOT)%.cpp=$(OBJ_ROOT)/%.o)
OBJS_CPP      := $(sort $(OBJS_CPP))
OBJS_CPP_DIR  := $(dir $(OBJS_CPP))
SRC_CPP_DEPS  := $(OBJS_CPP:%.o=%.d)

# C Source
SRCS_C        := $(shell find $(SRC_ROOT) -name '*.c')
SRCS_C        := $(sort $(SRCS_C))

OBJS_C        := $(SRCS_C:$(SRC_ROOT)%.c=$(OBJ_ROOT)/%.o)
OBJS_C        := $(sort $(OBJS_C))
OBJ_C_DIR     := $(dir $(OBJS_C))
SRC_C_DEPS    := $(OBJS_C:%.o=%.d)

CreateDir = $(shell [ -d $1 ] || mkdir -p $1 || echo ":mkdir '$1' fail")

# Create Object and Release Directory
CreateResult :=
dummy := $(call CreateDir, $(OBJ_ROOT))
dummy += $(call CreateDir, $(EXE_ROOT))
dummy += $(foreach dir, $(OBJS_CPP_DIR), CreateResult += $(call CreateDir, $(dir)))
dummy += $(foreach dir, $(OBJ_C_DIR), CreateResult += $(call CreateDir, $(dir)))
ifneq ($(strip CreateResult),)
    err = $(error $(CreateResult))
endif

# Compile Define
COMPILE_CPP    = $(CXX) -fPIC $(PDT_DEFS) $(CFLAGS) $(CXXFLAGS) -c "$<" -o "$@" $(INCLUDE_PATH)
COMPILEDEP_CPP = $(CXX) -MM "$<"  $(PDT_DEFS) $(CFLAGS) $(CXXFLAGS) $(INCLUDE_PATH)
COMPILE_C    = $(CC) -fPIC $(PDT_DEFS) $(CFLAGS) -c "$<" -o "$@" $(INCLUDE_PATH)
COMPILEDEP_C = $(CC) -MM "$<"  $(PDT_DEFS) $(CFLAGS) $(INCLUDE_PATH)
LINK = $(CXX) $(APP_CFLAGS)

.PHONY: all lib clean distclean
all: lib success

success:
	@echo -e "\e[0;32;1m--compiling '$(notdir $(LIB))' finished\e[0;36;1m" 
	@echo -e "\e[0m" 

$(OBJS_CPP): $(OBJ_ROOT)/%.o :$(SRC_ROOT)/%.cpp
	@echo " [CXX ] " $(notdir $@) " "
	@$(COMPILE_CPP)

$(SRC_CPP_DEPS): $(OBJ_ROOT)/%.d : $(SRC_ROOT)/%.cpp
	@echo " [DEPS] " $(notdir $@)
	@set -e;$(COMPILEDEP_CPP) > $@.$$$$; \
	sed 's,.*\.o[ :]*,$(@:%.d=%.o) $@ : ,g' < $@.$$$$ > $@;rm -f $@.$$$$

$(OBJS_C): $(OBJ_ROOT)/%.o :$(SRC_ROOT)/%.c
	@echo " [CC  ] " $(notdir $@) " "
	@$(COMPILE_C)

$(SRC_C_DEPS): $(OBJ_ROOT)/%.d : $(SRC_ROOT)/%.c
	@echo " [DEPS] " $(notdir $@)
	@set -e;$(COMPILEDEP_C) > $@.$$$$; \
	sed 's,.*\.o[ :]*,$(@:%.d=%.o) $@ : ,g' < $@.$$$$ > $@;rm -f $@.$$$$

lib: $(SRC_C_DEPS) $(OBJS_C) $(SRC_CPP_DEPS) $(OBJS_CPP)
	@echo " [LIB ] " $(notdir $@)
	@$(CC) -shared -fPIC -o $(LIB) $(OBJS_C) $(OBJS_CPP) $(CFLAGS) $(LINK_FLAGS)

target: $(SRC_C_DEPS) $(OBJS_C) $(SRC_CPP_DEPS) $(OBJS_CPP)
	@echo " [LINK]  $(notdir $(EXE))"
	@$(LINK) -o $(EXE) -Wl,--start-group $(OBJS_C) $(OBJS_CPP) $(LINK_FLAGS) -funwind-tables -rdynamic  $(APP_LIBS) -Wl,--end-group

clean:
	@rm -rf $(LIB)
	@rm -rf $(EXE)
	@rm -f $(OBJS_CPP)
	@rm -f $(OBJS_C)
	@rm -rf $(OBJ_ROOT)
